# File：反射案例.py
# Author：信思智学
# Date ：2025/8/7 14:56
# Info： 模拟购物商城   t_menu
# 管理员  卖家  买家
class Manager:
    role_menu = [
        ("【系统管理】","systemManager"),
        ("【权限管理】","roleManager"),
        ("【密码管理】","passwordManager"),
    ]

    def __init__(self,name):
        self.name = name

    def systemManager(self):
        print(self.name + "执行了系统管理功能...")

    def roleManager(self):
        print(self.name + "执行了权限管理功能...")

    def passwordManager(self):
        print(self.name + "执行了密码修改管理功能...")

class Seller:
    role_menu = [
        ("【商品上架】", "goodsAdd"),
        ("【商品下架】", "goodsDelete"),
        ("【商品发货】", "goodsSend"),
    ]

    def __init__(self, name):
        self.name = name

    def goodsAdd(self):
        print(self.name + "执行了商品上架功能...")

    def goodsDelete(self):
        print(self.name + "执行了商品下架功能...")

    def goodsSend(self):
        print(self.name + "执行了商品发货功能...")

class Buyer:
    role_menu = [
        ("【商品查看】", "goodsInfo"),
        ("【商品下单】", "goodsOrder"),
        ("【商品收货】", "goodsRecv"),
    ]

    def __init__(self, name):
        self.name = name

    def goodsInfo(self):
        print(self.name + "执行了商品查看功能...")

    def goodsOrder(self):
        print(self.name + "执行了商品下单功能...")

    def goodsRecv(self):
        print(self.name + "执行了商品收货功能...")

# 统一的登录入口
def login():
    username = input("请输入用户名...")
    password = input("请输入密码...")

    with open("userrole") as file:
        for line in file:
            username1,password1,role = line.strip().split(",")
            if username1 == username and password1 == password:
                print("登录成功")
                return username1,role

import sys
def main():
    user,role = login()
    print(role)
    #获取当前模块
    myModel = sys.modules["__main__"]
    # 获取类本身
    cls = getattr(myModel,role)

    # 反射  类创建对象
    obj = cls(user)

    # 获取类属性
    role_menu = cls.role_menu

    while 1:
        for i, menu in enumerate(role_menu, 1):
            print(i, menu[0])

        num = int(input("请输入对应的菜单编号..."))

        menu_name = role_menu[num-1][1]

        # 通过反射执行方法
        getattr(obj,menu_name)()


main()







